const crypto = require('crypto');

const sha256 = function(message, secret = '', encoding) {
	const hmac = crypto.createHmac('sha256', secret)
	return hmac.update(message).digest(encoding)
}

const getHash = function(message, encoding = 'hex') {
	const hash = crypto.createHash('sha256')
	return hash.update(message).digest(encoding)
}

const getDate = function(timestamp) {
	const date = new Date(timestamp * 1000)
	const year = date.getUTCFullYear()
	const month = ('0' + (date.getUTCMonth() + 1)).slice(-2)
	const day = ('0' + date.getUTCDate()).slice(-2)
	return `${year}-${month}-${day}`
}

/**
 * 停顿输出
 @param value 	输入值
 * @param type 		转换类型
 */
const pauseVal = function(value,type = "wx"){
	if(type === "wx"){
		if (value < 50 || value > 10000) {
			uni.showToast({
				title:"输入有误",
				icon:'error'
			})
			return undefined
		}
		switch((value % 1000)){
			case 0 :
				value = (value / 1000) + "s"
				break;
			default:
				value = value + "ms"
		}
		return value
	}
}

module.exports = {
	sha256,
	getHash,
	getDate,
	pauseVal,
}